<?php
namespace Cyfy\Modules\Comments;

use \Cyfy\Modules\BasicContent\Content;
use \Cyfy\Modules\Users\Current;
use \Cyfy\Modules\Users\User;


/**
 * Guest Comments module.
 *
 * This module include_onces an ordinary Guest Comments.
 * Users can insert entries, if they have permission.
 * The URL is /Comments by default, you can change it by modifying filesystem.xml and this class constants.
 * When changing path, the module must be set to DISABLED!!! Otherwise it won't change the filesystem.
 */
class Module extends \Cyfy\Module
{
    /**
     * Default entries on page, can be changed from administration.
     */
    const ON_PAGE = 10;
    
    /**
     * Method handling AJAX calls.
     *
     * Available methods are:
     * <ul>
     *    <li>'entries' - returns list of entries as HTML, starting on page 1</li>
     * </ul>
     *
     * @param string Called method name.
     * @return mixed AJAX callback result or FALSE.
     */
    static public function ajax($name)
    {
        switch($name)
        {
            case 'entries':
                return self :: ajaxEntries($_POST["commentModule"], $_POST["commentId"]);
                
            default:
                return false;
        }
    }
    
    
    /**
     * Returns all entries.
     * @return string Comments entries.
     */
    static public function getComments($module, $id, $tag = "h3")
    {
        
        // FORM
        if(\Cyfy\Cyfy :: permission('Comments', 'add new'))
        {
            $form = new Forms\NewEntry();
            $form -> setTemp("id", $id);
            $form -> setTemp("module", $module);
            $c = new \Cyfy\Modules\Objects\Collapsible();
            
            $c -> setClass('commentsNewCollapsibleForm');
            $c -> setData(self :: t(100), $form -> show(), true);
            
            $output .= $c -> show() . "<br />";            
        }
        $output .= "<{$tag}>" . self :: t(10) . "</{$tag}>";
        // ENTRIES
        $pager = new \Cyfy\Modules\DibiPager\DibiPager('comments', self :: ON_PAGE);		
        $pager -> ajaxCall('Comments','entries', '#comments' ,'commentModule=' . $module . "&commentId=" . $id);
        
        $output .= '<div id=\'comments\'>' . self :: ajaxEntries($module, $id) . '</div>';
        $output .= $pager -> pager("SELECT count([cid]) FROM [:cyfy:Comments] WHERE [module] = '$module' AND id = '$id'");
        return $output;
    }
  
  
    /**
     * Returns all entries. This method is used by ordinary GET as well as by AJAX callback.
     * @return string Comments entries.
     */
    static private function ajaxEntries($module, $id)
    {
        // pager declaration and ajax
        $pager = new \Cyfy\Modules\DibiPager\DibiPager('comments', self :: ON_PAGE);		
        $pager -> ajaxCall('Comments','entries', '#comments');
        
        
        // pager query
        $result = $pager -> query('SELECT * FROM [:cyfy:Comments] WHERE [module] = %s', $module, ' AND id = %s', $id, ' ORDER BY [cid] DESC');

		// if no results, exit with no entries found
		if(count($result) == 0){
			return \Cyfy\Message :: get( self :: t(20), \Cyfy\Message::WARNING);
		}
		 
        // odd and even setting for nice colours
        $evenOdd = 'odd'; 
        
        // entries itetator
		foreach($result as $e)
		{
            // bc
            $bc = Content :: load($e["bcid"]);
            if(!$bc)
            {
                \dibi :: query('DELETE FROM [:cyfy:Comments] WHERE [bcid] = %s', $e["bcid"]);
                continue;
            }
            
            
            // odd & even settings
            $evenOdd = ($evenOdd == 'even') ? 'odd' : 'even';
            
            
            // time format
            $time = \Cyfy\Modules\Locale\Locale :: getTime($bc -> getUpdated());
            
            
            $title = "  <h3 class=\"title\">" . $bc -> getTitle() . "</h3> ";
            $text = $bc -> getContent();
         
            $name = $e["user"];
            if($bc -> getCreator() != 0)
            {
                $user = \Cyfy\Modules\Users\User :: load($bc -> getCreator());
                if($user)
                {  
                    $name = $user -> name;
                }
            }
            
            $bcstr = $bc -> get("h3" ,false, false);
            
            // building a entry... templates oneday??
$output .= <<<ENTRY
<div class="comment {$evenOdd}"> 
    <div class="info"> 
        <p class="time">{$time}</p> 
        <p class="user">{$name}</p> 
    </div> 
    <div class="text">
        {$bcstr}
    </div>             
</div>             
ENTRY;
        }
        return $output;
    }
}
